
using System;
using System.Collections;
using TienIch.Facade;
using TienIch.Model;
using System.Data;
namespace TienIch.Business
{
	public class DepartmentBO : BaseBO
	{
		private DepartmentFacade facade = DepartmentFacade.Instance;
		protected static DepartmentBO instance = new DepartmentBO();

		protected DepartmentBO()
		{
			this.baseFacade = facade;
		}

		public static DepartmentBO Instance
		{
			get { return instance; }
		}
	
		public static DataTable KhoiTaoGrid()
        {
			DataTable dt = new DataTable();
			
			dt.Columns.Add("ID", typeof(int ));
			dt.Columns.Add("Code", typeof(string ));
			dt.Columns.Add("Name", typeof(string ));
			dt.Columns.Add("Description", typeof(string ));
			dt.Columns.Add("CreatedBy", typeof(string ));
			dt.Columns.Add("CreatedDate", typeof(DateTime ));
			dt.Columns.Add("UpdatedBy", typeof(string ));
			dt.Columns.Add("UpdatedDate", typeof(DateTime ));
			dt.Columns.Add("Status", typeof(int ));
			dt.Columns.Add("Email", typeof(string ));
			dt.Columns.Add("HeadofDepartment", typeof(int ));
			dt.Columns.Add("IDCompany", typeof(int ));
			return dt;
		}
		
		public static void SaveToTable(DataTable dt)
		{
			DataTable dtRemine;
			dtRemine = dt.GetChanges();
			if (dtRemine != null)
			{
				foreach (DataRow item in dtRemine.Rows)
				{
					DepartmentModel model = new DepartmentModel();
					
					model.ID =Convert.ToInt32(item["ID"]);
					model.Code =Convert.ToString(item["Code"]);
					model.Name =Convert.ToString(item["Name"]);
					model.Description =Convert.ToString(item["Description"]);
					model.CreatedBy =Convert.ToString(item["CreatedBy"]);
					model.CreatedDate =Convert.ToDateTime(item["CreatedDate"]);
					model.UpdatedBy =Convert.ToString(item["UpdatedBy"]);
					model.UpdatedDate =Convert.ToDateTime(item["UpdatedDate"]);
					model.Status =Convert.ToInt32(item["Status"]);
					model.Email =Convert.ToString(item["Email"]);
					model.HeadofDepartment =Convert.ToInt32(item["HeadofDepartment"]);
					model.IDCompany =Convert.ToInt32(item["IDCompany"]);
					if (item["ID"] == DBNull.Value) //add new
					{
						DepartmentBO.Instance.Insert(model);
					}
					else //Update
					{
						model.ID = Convert.ToInt32(item["ID"].ToString());
						DepartmentBO.Instance.Update(model);
					}
				}
			}
		}
		
		
	}
}
	